package com.weiaibenpao.demo.chislim.presenter;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.base.BasePresenter;
import com.weiaibenpao.demo.chislim.bean.IsLikeBean;
import com.weiaibenpao.demo.chislim.bean.MomentListBean;
import com.weiaibenpao.demo.chislim.retrofit.RetrofitUtils;
import com.weiaibenpao.demo.chislim.retrofit.rx.NetWorkTransformer;
import com.weiaibenpao.demo.chislim.retrofit.rx.SimpleObserver;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.MomentListView;

/**
 * Created by Administrator on 2018/1/4.
 */

public class MomentListPresenter extends BasePresenter<MomentListView> {

    public void getLatestMoments(int userId,int page){
        Logger.e("getLatestMoments----->");
        RetrofitUtils
                .getAPIService()
                .getLatestMoments(userId,page, Constant.DEFAULT_PAGE_SIZE)
                .compose(new NetWorkTransformer<MomentListBean>())
                .subscribe(new SimpleObserver<MomentListBean>(mCompositeDisposable) {
                    @Override
                    public void call(MomentListBean bean) {
                        getView().onGetMomentListSuccess(bean);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                       if(getView() != null)
                        getView().onGetMomentListFailed(message);
                    }
                });
    }

    public void getCaredMoments(int userId,int page){
        Logger.e("getCaredMoments----->");
        RetrofitUtils
                .getAPIService()
                .getCaredMoments(userId,page, Constant.DEFAULT_PAGE_SIZE)
                .compose(new NetWorkTransformer<MomentListBean>())
                .subscribe(new SimpleObserver<MomentListBean>(mCompositeDisposable) {
                    @Override
                    public void call(MomentListBean bean) {
                        getView().onGetMomentListSuccess(bean);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                        getView().onGetMomentListFailed(message);
                    }
                });
    }


    public void likeOrUnLike(int userId, int momentId, final int position){
        Logger.e("likeOrUnLike----->");
        RetrofitUtils
                .getAPIService()
                .likeOrUnLike(userId,momentId)
                .compose(new NetWorkTransformer<IsLikeBean>())
                .subscribe(new SimpleObserver<IsLikeBean>(mCompositeDisposable) {
                    @Override
                    public void call(IsLikeBean bean) {
                        getView().onLikeOrUnlikeSuccess(bean.getIsLike(),position);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                        getView().onLikeOrUnlikeFailed(message);
                    }
                });
    }

    public void report(int userId,int targetId,int type,String content){
        Logger.e("report-----> user id "+userId+"--targetId "+targetId);
        RetrofitUtils
                .getAPIService()
                .report(userId,targetId,type,content)
                .compose(new NetWorkTransformer())
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object bean) {
                        getView().onReportSuccess();
                    }

                });
    }


    public void deleteMoment(int userId, int momentId, final int position){
        Logger.e("deleteMoment-----> user id "+userId+"--momentId "+momentId);
        RetrofitUtils
                .getAPIService()
                .deleteMoment(userId,momentId)
                .compose(new NetWorkTransformer())
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object bean) {
                        getView().onDeleteSuccess(position);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                        getView().onDeleteFailed(message);
                    }
                });
    }

}
